package com.hcbd.tdwt.ui.xingyu.vo;

import com.hcbd.tdwt.base.Constant;
import com.hcbd.tdwt.util.AppDatabase;
import com.hcbd.tdwt.util.CharacterParser;
import com.hcbd.tdwt.util.DateUtils;
import com.raizlabs.android.dbflow.annotation.Column;
import com.raizlabs.android.dbflow.annotation.PrimaryKey;
import com.raizlabs.android.dbflow.annotation.Table;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.structure.BaseModel;

import java.util.Calendar;
import java.util.Locale;

@Table(database = AppDatabase.class)
public class BoxContact extends BaseModel implements java.io.Serializable {

    @PrimaryKey(autoincrement = true)
    private int id;

    //用户名
    @Column
    private String name;
    //终端id
    @Column
    private String boxId;
    //最近一次聊天的时间
    @Column
    private String recentCommunicationTime;
    //最近一次聊天内容
    @Column
    private String lastTimeContent;
    //未读消息数
    @Column
    private int unReadNewsNum = 0;
    //联系人类型，0：陌生人，1：好友
    @Column
    private String contactType = "0";

    //0未关注 1关注
    @Column
    private int isPayAttention;
    //经度
    @Column
    private String longitude;
    //纬度
    @Column
    private String latitude;
    @Column//姓名首字母
    private String sortLetter;

    public static void upData(String boxId, String name, String content,int isPay){
        BoxContact oldBoxContact= SQLite.select().from(BoxContact.class).where(BoxContact_Table.boxId.eq(boxId)).querySingle();
        if(oldBoxContact==null){
            BoxContact contact = new BoxContact();
            contact.setBoxId(boxId);
            contact.setName(name);
            contact.setIsPayAttention(isPay);
            contact.setLastTimeContent(content);
            contact.setSortName();
            contact.setUnReadNewsNum(contact.getUnReadNewsNum());
            contact.setRecentCommunicationTime(DateUtils.getDateLongSerial());
            contact.insert();
        }else{
            oldBoxContact.setName(name);
            oldBoxContact.setUnReadNewsNum(oldBoxContact.getUnReadNewsNum());
            oldBoxContact.setIsPayAttention(isPay);
            oldBoxContact.setRecentCommunicationTime(DateUtils.getDateLongSerial());
            oldBoxContact.update();
        }

    }

    /**
     * 通过终端id保存最近联系人
     *
     * @param boxId 消息实体

     */
    public static void cacheFromBoxId(String boxId) {
        //保存联系人
        BoxContact oldBoxContact= SQLite.select().from(BoxContact.class).where(BoxContact_Table.boxId.eq(boxId)).querySingle();
        if(oldBoxContact!=null){
            oldBoxContact.setUnReadNewsNum(0);
            oldBoxContact.update();
        }
    }
    /**
     * 通过终端id保存最近联系人
     *
     * @param boxId 消息实体
     * @param isRead      是否已读
     */
    public static void cacheFromBoxId(String boxId,int type,String newContent,String lon,String lat,boolean isRead) {
        if(Constant.AUDIO==type) {
            newContent="语音";
        }else if(Constant.PICTURE==type){
            newContent="图片";
        }
        //保存联系人
        BoxContact oldBoxContact= SQLite.select().from(BoxContact.class).where(BoxContact_Table.boxId.eq(boxId)).querySingle();
        if (oldBoxContact == null) {
            BoxContact contact = new BoxContact();
            contact.setBoxId(boxId);
            contact.setName(boxId);
            contact.setLongitude(lon);
            contact.setLatitude(lat);
            contact.setSortName();
            if(newContent!=null) {
                contact.setLastTimeContent(newContent);
            }
            contact.setRecentCommunicationTime(DateUtils.CalendarToString(Calendar.getInstance(), null));
            if (isRead) {
                contact.setUnReadNewsNum(0);
            } else {
                contact.setUnReadNewsNum(contact.getUnReadNewsNum() + 1);
            }
            contact.insert();
        } else {
            if(newContent!=null) {
                oldBoxContact.setLastTimeContent(newContent);
            }
            if(lon!=null&&!"".equals(lon)) {
                oldBoxContact.setLongitude(lon);
                oldBoxContact.setLatitude(lat);
            }
            oldBoxContact.setRecentCommunicationTime(DateUtils.CalendarToString(Calendar.getInstance(), null));
            if (isRead) {
                oldBoxContact.setUnReadNewsNum(0);
            } else {
                oldBoxContact.setUnReadNewsNum(oldBoxContact.getUnReadNewsNum() + 1);
            }
            oldBoxContact.update();
        }
    }

    /**
     * 通过终端id保存最近联系人
     *
     * @param boxId 消息实体
     * @param isRead      是否已读
     */
    public static void cacheFromBoxId(String boxId,int type,String newContent,boolean isRead) {
        if(Constant.AUDIO==type) {
            newContent="语音";
        }else if(Constant.PICTURE==type){
            newContent="图片";
        }
        //保存联系人
        BoxContact oldBoxContact= SQLite.select().from(BoxContact.class).where(BoxContact_Table.boxId.eq(boxId)).querySingle();
        if (oldBoxContact == null) {
            BoxContact contact = new BoxContact();
            contact.setBoxId(boxId);
            contact.setName(boxId);
            contact.setSortName();
            contact.setLongitude("0");
            contact.setLatitude("0");
            contact.setLastTimeContent(newContent);
            contact.setRecentCommunicationTime(DateUtils.CalendarToString(Calendar.getInstance(), null));
            if (isRead) {
                contact.setUnReadNewsNum(0);
            } else {
                contact.setUnReadNewsNum(contact.getUnReadNewsNum() + 1);
            }
            contact.insert();
        } else {
            oldBoxContact.setLastTimeContent(newContent);
            oldBoxContact.setRecentCommunicationTime(DateUtils.getDateLongSerial());
            if (isRead) {
                oldBoxContact.setUnReadNewsNum(0);
            } else {
                oldBoxContact.setUnReadNewsNum(oldBoxContact.getUnReadNewsNum() + 1);
            }
            oldBoxContact.update();
        }
    }
    /**
     * 在搜索筛选时候的条件（按名称，北斗号，手机号）
     */
    private ShowIndex showIndex;

    public ShowIndex getShowIndex() {
        return showIndex;
    }

    public void setShowIndex(ShowIndex showIndex) {
        this.showIndex = showIndex;
    }







    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public int getIsPayAttention() {
        return isPayAttention;
    }

    public void setIsPayAttention(int isPayAttention) {
        this.isPayAttention = isPayAttention;
    }

    public void setLastTimeContent(String lastTimeContent) {
        this.lastTimeContent = lastTimeContent;
    }
    public String getLastTimeContent() {
        return lastTimeContent;
    }

    public String getSortLetter() {
        return sortLetter;
    }


    public void setSortLetter(String sortLetter) {
        this.sortLetter = sortLetter;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public enum ShowIndex {
        NAME, BOXID
    }


    public BoxContact() {

    }

    public void setName(String name) {
        this.name = name;
    /*    String pinyin = CharacterParser.getInstance().getSelling(name);
        // 正则表达式，判断首字母是否是英文字母
        String sortString = pinyin.substring(0, 1).toUpperCase(Locale.ENGLISH);
        if (sortString.matches("[A-Z]")) {
            this.sortLetter = sortString;
        } else {
            this.sortLetter = "#";
        }*/
    }



    public String getName() {
        return name;
    }

    public String getBoxId() {
        return boxId;
    }

    public void setBoxId(String boxId) {
        this.boxId = boxId;
    }

  /*  public String getSortLetter() {
        return sortLetter;
    }

    public void setSortLetter(String sortLetter) {
        String pinyin = CharacterParser.getInstance().getSelling(sortLetter);
        // 正则表达式，判断首字母是否是英文字母
        String sortString = pinyin.substring(0, 1).toUpperCase(Locale.ENGLISH);
        if (sortString.matches("[A-Z]") || sortString.equals("+")) {
            this.sortLetter = sortString;
        } else {
            this.sortLetter = "#";
        }
    }*/

    public String getRecentCommunicationTime() {
        return this.recentCommunicationTime;
    }

    public void setRecentCommunicationTime(String recentCommunicationTime) {
        this.recentCommunicationTime = recentCommunicationTime;
    }



    public int getUnReadNewsNum() {
        return unReadNewsNum;
    }

    public void setUnReadNewsNum(int unReadNewsNu) {
        this.unReadNewsNum = unReadNewsNu;
    }

    public String getContactType() {
        return contactType;
    }

    public void setContactType(String contactType) {
        this.contactType = contactType;
    }

    public  void setSortName(){
        String pinyin;
        if(null==name||"".equals(name)){
            pinyin = CharacterParser.getInstance().getSelling(boxId);
        }else{
            pinyin = CharacterParser.getInstance().getSelling(name);
        }
        // 正则表达式，判断首字母是否是英文字母
        String sortString = pinyin.substring(0, 1).toUpperCase(Locale.ENGLISH);
        if (sortString.matches("[A-Z]")) {
            this.sortLetter = sortString;
        } else {
            this.sortLetter = "#";
        }
    }

    @Override
    public String toString() {
        return "BoxContact{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", boxId='" + boxId + '\'' +
                ", sortLetter='" + '\'' +
                ", recentCommunicationTime='" + recentCommunicationTime + '\'' +
                ", lastTimeContent='" + lastTimeContent + '\'' +
                ", unReadNewsNum=" + unReadNewsNum +
                ", contactType='" + contactType + '\'' +
                ", showIndex=" + showIndex +
                '}';
    }
}
